﻿using IGF.Utils;
using LikeProxy.Moduels;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LikeProxy.Controls
{
    public partial class WhistleControl : UserControl
    {
        private WebView2 whistle;
        public WhistleControl()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.whistle = new WebView2()
            {
                Dock = DockStyle.Fill
            };
            this.Controls.Add(this.whistle);

        }
        public static string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取本机IP出错:" + ex.Message);
                return "";
            }
        }

        private void WhistleControl_Load(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                string outStr = CMDHelper.Execute("w2 start --socksPort 1983", 10);
                Console.WriteLine(outStr);
                this.InvokeIfRequired(() =>
                {
                    this.whistle.Source = new Uri("http://127.0.0.1:8899");
                });

            });
        }
    }
}
